package com.springcloud.netflixribbon.controller;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping(value = "/ribbon")
@RestController
public class LoadBalanceController {
    private Logger logger = LoggerFactory.getLogger(LoadBalanceController.class);

    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/toLoadBalance")
    @HystrixCommand(fallbackMethod = "ribbonHystrixError")
    public void toLoadBalance(){
        for (int i = 0; i < 10; i++){
            String str = restTemplate.getForObject("http://eureka-client/testRabbion",String.class);
            logger.info(str);
        }
    }

    public void ribbonHystrixError(){
        logger.info("ribbon error");
    }

    @GetMapping(value = "/testZuul")
    @ResponseBody
    public String testZuul(){
        return "这是netflix-ribbon子模块";
    }
}
